Recall that HMC simulates the frictionless flow of a particle on a surface. In any given transition, which is just a single flick of the particle, the total energy at the start should be equal to the total energy at the end. That’s how energy in a closed system works. And in a purely mathematical system, the energy is always conserved correctly. It’s just a fact about the physics.
But in a numerical system, it might not be. Sometimes the total energy is not the same at the end as it was at the start. In these cases, the energy is divergent. How can this happen? It tends to happen when the posterior distribution is very steep in some region of parameter space. Steep changes in probability are hard for a discrete physics simulation to follow. When that happens, the algorithm notices by comparing the energy at the start to the energy at the end. When they don’t match, it indicates numerical problems exploring that part of the posterior distribution.
centered parameterization
In his lecture, McElreath uses CENTERED PARAMETERIZATION to demonstrate divergent transitions. A very simple example:
This expression is centered because one set of priors (the priors for \(x\)) are centered around another prior (the prior for \(\nu\)). It’s intuitive, but this can cause a lot of problems with Stan, which is probably why McElreath used this for his example. In short, when there is limited data within our groups or the population variance is small, the parameters \(x\) and \(\nu\) become highly correlated. This geometry is challenging for MCMC to sample. (Think of a long and narrow groove, not a bowl, for your Hamiltonian skateboard.)
Code
set.seed(1)# plot the likelihoodsps <-seq( from=-4, to=4, length.out=200) # possible parameter values for both x and nucrossing(nu = ps, x=ps) %>%#every possible combination of nu and xmutate(likelihood_nu =dnorm(nu, 0, 3),likelihood_x =dnorm(x, 0, exp(nu)),joint_likelihood = likelihood_nu*likelihood_x ) %>%ggplot( aes(x=x, y=nu, fill=joint_likelihood) ) +geom_raster() +scale_fill_viridis_c() +guides(fill = F)
The way to fix this is by using an uncentered parameterization:
\[\begin{align*}
x &= z\times \text{exp}(\nu) \\
z &\sim \text{Normal}(0, 1) \\
\nu &\sim \text{Normal}(0, 3) \\
\end{align*}\]
Code
set.seed(1)# plot the likelihoodsps <-seq( from=-4, to=4, length.out=200) # possible parameter values for both x and nucrossing(nu = ps, z=ps) %>%#every possible combination of nu and xmutate(likelihood_nu =dnorm(nu, 0, 3),likelihood_z =dnorm(z, 0, 1),joint_likelihood = likelihood_nu*likelihood_z ) %>%ggplot( aes(x=z, y=nu, fill=joint_likelihood) ) +geom_raster() +scale_fill_viridis_c() +guides(fill = F)
It’s an important point, except the issues of centered parameterization are so prevalent1, that brms generally doesn’t allow centered parameterization (with some exceptions). So we can’t recreate the divergent transition situation that McElreath demonstrates in his lecture.
McElreath describes the problem of fertility in Bangladesh as such:
Family: bernoulli
Links: mu = logit
Formula: use.contraception ~ 1 + (1 | district)
Data: d (Number of observations: 1934)
Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup draws = 4000
Multilevel Hyperparameters:
~district (Number of levels: 60)
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sd(Intercept) 0.52 0.09 0.37 0.70 1.00 1374 1915
Regression Coefficients:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept -0.54 0.09 -0.72 -0.37 1.00 1998 2342
Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
# ---- set population-level parameters -----a <-3.5# average morning wait timeb <- (-1) # average difference afternoon wait timesigma_a <-1# std dev in interceptssigma_b <-0.5# std dev in slopesrho <- (-0.7) #correlation between intercepts and slopes# ---- create vector of means ----Mu <-c(a, b)# ---- create matrix of variances and covariances ----sigmas <-c(sigma_a,sigma_b) # standard deviationsRho <-matrix( c(1,rho,rho,1) , nrow=2 ) # correlation matrix# now matrix multiply to get covariance matrixSigma <-diag(sigmas) %*% Rho %*%diag(sigmas)# ---- simulate intercepts and slopes -----N_cafes =20library(MASS)set.seed(5)vary_effects <-mvrnorm( n=N_cafes, mu = Mu, Sigma=Sigma)a_cafe <- vary_effects[, 1]b_cafe <- vary_effects[, 2]# ---- simulate observations -----set.seed(22)N_visits <-10afternoon <-rep(0:1,N_visits*N_cafes/2)cafe_id <-rep( 1:N_cafes , each=N_visits )mu <- a_cafe[cafe_id] + b_cafe[cafe_id]*afternoonsigma <-0.5# std dev within cafeswait <-rnorm( N_visits*N_cafes , mu , sigma )d <-data.frame( cafe=cafe_id , afternoon=afternoon , wait=wait )
a simulation note from RM
In this exercise, we are simulating data from a generative process and then analyzing that data with a model that reflects exactly the correct structure of that process. But in the real world, we’re never so lucky. Instead we are always forced to analyze data with a model that is MISSPECIFIED: The true data-generating process is different than the model. Simulation can be used however to explore misspecification. Just simulate data from a process and then see how a number of models, none of which match exactly the data-generating process, perform. And always remember that Bayesian inference does not depend upon data-generating assumptions, such as the likelihood, being true. Non-Bayesian approaches may depend upon sampling distributions for their inferences, but this is not the case for a Bayesian model. In a Bayesian model, a likelihood is a prior for the data, and inference about parameters can be surprisingly insensitive to its details.